package com.dingding.manager.core.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.hibernate.HibernateException;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.dingding.manager.common.exception.BussinessException;
import com.dingding.manager.common.exception.ErrorTips;
import com.dingding.manager.common.exception.enums.BizExceptionEnum;

/**
 * @Description 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 * @author fengshuonan
 * @date 2016年11月12日 下午3:19:56
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description 拦截业务异常
	 * @author fengshuonan
	 */
	@ExceptionHandler(BussinessException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody ErrorTips notFount(BussinessException e) {
		log.warn("拦截到业务异常:" + e.getMessage());
		return new ErrorTips(e.getCode(), e.getMessage());
	}

	/**
	 * @Description 拦截未知的运行时异常
	 * @author fengshuonan
	 */
	@ExceptionHandler(HibernateException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody ErrorTips notFount(HibernateException e) {
		log.error("拦截到数据库异常:" + e.getMessage(),e);
		return new ErrorTips(BizExceptionEnum.SERVER_ERROR);
	}

	/**
	 * @Description 找不到用户
	 * @author fengshuonan
	 */
	@ExceptionHandler(UnknownAccountException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public String unKnowAccount(UnknownAccountException e, Model model) {
		
		model.addAttribute("error", "帐号或密码错误");
		return "login";
	}

	/**
	 * @Description 账号被禁用
	 * @author fengshuonan
	 */
	@ExceptionHandler(DisabledAccountException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public String disabledAccount(DisabledAccountException e, Model model) {
		model.addAttribute("error", "账号被禁用");
		return "login";
	}
	
	/**
	 * @Description 账号被禁用
	 * @author sunzhicheng
	 *//*
	@ExceptionHandler(DisabledAccountException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public  @ResponseBody ErrorTips disabledAccount(DisabledAccountException e, Model model) {
		return new ErrorTips(BizExceptionEnum.DISABLED_ACCOUNT);
	}*/
	
	/**
	 * @Description 用户名或密码错误
	 * @author fengshuonan
	 */
	@ExceptionHandler(IncorrectCredentialsException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public String authentication(IncorrectCredentialsException e, Model model) {
		model.addAttribute("error", "帐号或密码错误");
		return "login";
	}
	
	
	/**
	 * @Description 权限认证异常
	 * @author fengshuonan
	 */
	@ExceptionHandler(AuthenticationException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public String authentication(AuthenticationException e, Model model) {
		model.addAttribute("error", "访问资源受限");
		return "login";
	}
	
	/**
	 * @Description 拦截未知的运行时异常
	 * @author fengshuonan
	 */
	@ExceptionHandler(Throwable.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody ErrorTips notFount(Throwable e) {
		log.error("拦截到未知的异常:",e);
		return new ErrorTips(BizExceptionEnum.SERVER_ERROR);
	}
}
